<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
</head>
<body>
<main role="main">
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a id="line.1">/*</a>
<span class="sourceLineNo">002</span><a id="line.2"> * Licensed to the Apache Software Foundation (ASF) under one or more</a>
<span class="sourceLineNo">003</span><a id="line.3"> * contributor license agreements.  See the NOTICE file distributed with</a>
<span class="sourceLineNo">004</span><a id="line.4"> * this work for additional information regarding copyright ownership.</a>
<span class="sourceLineNo">005</span><a id="line.5"> * The ASF licenses this file to You under the Apache License, Version 2.0</a>
<span class="sourceLineNo">006</span><a id="line.6"> * (the "License"); you may not use this file except in compliance with</a>
<span class="sourceLineNo">007</span><a id="line.7"> * the License.  You may obtain a copy of the License at</a>
<span class="sourceLineNo">008</span><a id="line.8"> *</a>
<span class="sourceLineNo">009</span><a id="line.9"> *      http://www.apache.org/licenses/LICENSE-2.0</a>
<span class="sourceLineNo">010</span><a id="line.10"> *</a>
<span class="sourceLineNo">011</span><a id="line.11"> * Unless required by applicable law or agreed to in writing, software</a>
<span class="sourceLineNo">012</span><a id="line.12"> * distributed under the License is distributed on an "AS IS" BASIS,</a>
<span class="sourceLineNo">013</span><a id="line.13"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</a>
<span class="sourceLineNo">014</span><a id="line.14"> * See the License for the specific language governing permissions and</a>
<span class="sourceLineNo">015</span><a id="line.15"> * limitations under the License.</a>
<span class="sourceLineNo">016</span><a id="line.16"> */</a>
<span class="sourceLineNo">017</span><a id="line.17">package org.apache.commons.dbutils;</a>
<span class="sourceLineNo">018</span><a id="line.18"></a>
<span class="sourceLineNo">019</span><a id="line.19">import org.apache.commons.dbutils.annotations.Column;</a>
<span class="sourceLineNo">020</span><a id="line.20"></a>
<span class="sourceLineNo">021</span><a id="line.21">import java.beans.BeanInfo;</a>
<span class="sourceLineNo">022</span><a id="line.22">import java.beans.IntrospectionException;</a>
<span class="sourceLineNo">023</span><a id="line.23">import java.beans.Introspector;</a>
<span class="sourceLineNo">024</span><a id="line.24">import java.beans.PropertyDescriptor;</a>
<span class="sourceLineNo">025</span><a id="line.25">import java.lang.reflect.Field;</a>
<span class="sourceLineNo">026</span><a id="line.26">import java.lang.reflect.InvocationTargetException;</a>
<span class="sourceLineNo">027</span><a id="line.27">import java.lang.reflect.Method;</a>
<span class="sourceLineNo">028</span><a id="line.28">import java.sql.ResultSet;</a>
<span class="sourceLineNo">029</span><a id="line.29">import java.sql.ResultSetMetaData;</a>
<span class="sourceLineNo">030</span><a id="line.30">import java.sql.SQLException;</a>
<span class="sourceLineNo">031</span><a id="line.31">import java.util.ArrayList;</a>
<span class="sourceLineNo">032</span><a id="line.32">import java.util.Arrays;</a>
<span class="sourceLineNo">033</span><a id="line.33">import java.util.HashMap;</a>
<span class="sourceLineNo">034</span><a id="line.34">import java.util.List;</a>
<span class="sourceLineNo">035</span><a id="line.35">import java.util.Map;</a>
<span class="sourceLineNo">036</span><a id="line.36">import java.util.ServiceLoader;</a>
<span class="sourceLineNo">037</span><a id="line.37"></a>
<span class="sourceLineNo">038</span><a id="line.38">/**</a>
<span class="sourceLineNo">039</span><a id="line.39"> * &lt;p&gt;</a>
<span class="sourceLineNo">040</span><a id="line.40"> * {@code BeanProcessor} matches column names to bean property names</a>
<span class="sourceLineNo">041</span><a id="line.41"> * and converts {@code ResultSet} columns into objects for those bean</a>
<span class="sourceLineNo">042</span><a id="line.42"> * properties.  Subclasses should override the methods in the processing chain</a>
<span class="sourceLineNo">043</span><a id="line.43"> * to customize behavior.</a>
<span class="sourceLineNo">044</span><a id="line.44"> * &lt;/p&gt;</a>
<span class="sourceLineNo">045</span><a id="line.45"> *</a>
<span class="sourceLineNo">046</span><a id="line.46"> * &lt;p&gt;</a>
<span class="sourceLineNo">047</span><a id="line.47"> * This class is thread-safe.</a>
<span class="sourceLineNo">048</span><a id="line.48"> * &lt;/p&gt;</a>
<span class="sourceLineNo">049</span><a id="line.49"> *</a>
<span class="sourceLineNo">050</span><a id="line.50"> * @see BasicRowProcessor</a>
<span class="sourceLineNo">051</span><a id="line.51"> *</a>
<span class="sourceLineNo">052</span><a id="line.52"> * @since DbUtils 1.1</a>
<span class="sourceLineNo">053</span><a id="line.53"> */</a>
<span class="sourceLineNo">054</span><a id="line.54">public class BeanProcessor {</a>
<span class="sourceLineNo">055</span><a id="line.55"></a>
<span class="sourceLineNo">056</span><a id="line.56">    /**</a>
<span class="sourceLineNo">057</span><a id="line.57">     * Special array value used by {@code mapColumnsToProperties} that</a>
<span class="sourceLineNo">058</span><a id="line.58">     * indicates there is no bean property that matches a column from a</a>
<span class="sourceLineNo">059</span><a id="line.59">     * {@code ResultSet}.</a>
<span class="sourceLineNo">060</span><a id="line.60">     */</a>
<span class="sourceLineNo">061</span><a id="line.61">    protected static final int PROPERTY_NOT_FOUND = -1;</a>
<span class="sourceLineNo">062</span><a id="line.62"></a>
<span class="sourceLineNo">063</span><a id="line.63">    /**</a>
<span class="sourceLineNo">064</span><a id="line.64">     * Set a bean's primitive properties to these defaults when SQL NULL</a>
<span class="sourceLineNo">065</span><a id="line.65">     * is returned.  These are the same as the defaults that ResultSet get*</a>
<span class="sourceLineNo">066</span><a id="line.66">     * methods return in the event of a NULL column.</a>
<span class="sourceLineNo">067</span><a id="line.67">     */</a>
<span class="sourceLineNo">068</span><a id="line.68">    private static final Map&lt;Class&lt;?&gt;, Object&gt; primitiveDefaults = new HashMap&lt;&gt;();</a>
<span class="sourceLineNo">069</span><a id="line.69"></a>
<span class="sourceLineNo">070</span><a id="line.70">    private static final List&lt;ColumnHandler&gt; columnHandlers = new ArrayList&lt;&gt;();</a>
<span class="sourceLineNo">071</span><a id="line.71"></a>
<span class="sourceLineNo">072</span><a id="line.72">    private static final List&lt;PropertyHandler&gt; propertyHandlers = new ArrayList&lt;&gt;();</a>
<span class="sourceLineNo">073</span><a id="line.73"></a>
<span class="sourceLineNo">074</span><a id="line.74">    /**</a>
<span class="sourceLineNo">075</span><a id="line.75">     * ResultSet column to bean property name overrides.</a>
<span class="sourceLineNo">076</span><a id="line.76">     */</a>
<span class="sourceLineNo">077</span><a id="line.77">    private final Map&lt;String, String&gt; columnToPropertyOverrides;</a>
<span class="sourceLineNo">078</span><a id="line.78"></a>
<span class="sourceLineNo">079</span><a id="line.79">    static {</a>
<span class="sourceLineNo">080</span><a id="line.80">        primitiveDefaults.put(Integer.TYPE, Integer.valueOf(0));</a>
<span class="sourceLineNo">081</span><a id="line.81">        primitiveDefaults.put(Short.TYPE, Short.valueOf((short) 0));</a>
<span class="sourceLineNo">082</span><a id="line.82">        primitiveDefaults.put(Byte.TYPE, Byte.valueOf((byte) 0));</a>
<span class="sourceLineNo">083</span><a id="line.83">        primitiveDefaults.put(Float.TYPE, Float.valueOf(0f));</a>
<span class="sourceLineNo">084</span><a id="line.84">        primitiveDefaults.put(Double.TYPE, Double.valueOf(0d));</a>
<span class="sourceLineNo">085</span><a id="line.85">        primitiveDefaults.put(Long.TYPE, Long.valueOf(0L));</a>
<span class="sourceLineNo">086</span><a id="line.86">        primitiveDefaults.put(Boolean.TYPE, Boolean.FALSE);</a>
<span class="sourceLineNo">087</span><a id="line.87">        primitiveDefaults.put(Character.TYPE, Character.valueOf((char) 0));</a>
<span class="sourceLineNo">088</span><a id="line.88"></a>
<span class="sourceLineNo">089</span><a id="line.89">        // Use a ServiceLoader to find implementations</a>
<span class="sourceLineNo">090</span><a id="line.90">        for (final ColumnHandler handler : ServiceLoader.load(ColumnHandler.class)) {</a>
<span class="sourceLineNo">091</span><a id="line.91">            columnHandlers.add(handler);</a>
<span class="sourceLineNo">092</span><a id="line.92">        }</a>
<span class="sourceLineNo">093</span><a id="line.93"></a>
<span class="sourceLineNo">094</span><a id="line.94">        // Use a ServiceLoader to find implementations</a>
<span class="sourceLineNo">095</span><a id="line.95">        for (final PropertyHandler handler : ServiceLoader.load(PropertyHandler.class)) {</a>
<span class="sourceLineNo">096</span><a id="line.96">            propertyHandlers.add(handler);</a>
<span class="sourceLineNo">097</span><a id="line.97">        }</a>
<span class="sourceLineNo">098</span><a id="line.98">    }</a>
<span class="sourceLineNo">099</span><a id="line.99"></a>
<span class="sourceLineNo">100</span><a id="line.100">    /**</a>
<span class="sourceLineNo">101</span><a id="line.101">     * Constructor for BeanProcessor.</a>
<span class="sourceLineNo">102</span><a id="line.102">     */</a>
<span class="sourceLineNo">103</span><a id="line.103">    public BeanProcessor() {</a>
<span class="sourceLineNo">104</span><a id="line.104">        this(new HashMap&lt;String, String&gt;());</a>
<span class="sourceLineNo">105</span><a id="line.105">    }</a>
<span class="sourceLineNo">106</span><a id="line.106"></a>
<span class="sourceLineNo">107</span><a id="line.107">    /**</a>
<span class="sourceLineNo">108</span><a id="line.108">     * Constructor for BeanProcessor configured with column to property name overrides.</a>
<span class="sourceLineNo">109</span><a id="line.109">     *</a>
<span class="sourceLineNo">110</span><a id="line.110">     * @param columnToPropertyOverrides ResultSet column to bean property name overrides</a>
<span class="sourceLineNo">111</span><a id="line.111">     * @since 1.5</a>
<span class="sourceLineNo">112</span><a id="line.112">     */</a>
<span class="sourceLineNo">113</span><a id="line.113">    public BeanProcessor(final Map&lt;String, String&gt; columnToPropertyOverrides) {</a>
<span class="sourceLineNo">114</span><a id="line.114">        super();</a>
<span class="sourceLineNo">115</span><a id="line.115">        if (columnToPropertyOverrides == null) {</a>
<span class="sourceLineNo">116</span><a id="line.116">            throw new IllegalArgumentException("columnToPropertyOverrides map cannot be null");</a>
<span class="sourceLineNo">117</span><a id="line.117">        }</a>
<span class="sourceLineNo">118</span><a id="line.118">        this.columnToPropertyOverrides = columnToPropertyOverrides;</a>
<span class="sourceLineNo">119</span><a id="line.119">    }</a>
<span class="sourceLineNo">120</span><a id="line.120"></a>
<span class="sourceLineNo">121</span><a id="line.121">    /**</a>
<span class="sourceLineNo">122</span><a id="line.122">     * Convert a {@code ResultSet} row into a JavaBean.  This</a>
<span class="sourceLineNo">123</span><a id="line.123">     * implementation uses reflection and {@code BeanInfo} classes to</a>
<span class="sourceLineNo">124</span><a id="line.124">     * match column names to bean property names.  Properties are matched to</a>
<span class="sourceLineNo">125</span><a id="line.125">     * columns based on several factors:</a>
<span class="sourceLineNo">126</span><a id="line.126">     * &amp;lt;br/&amp;gt;</a>
<span class="sourceLineNo">127</span><a id="line.127">     * &amp;lt;ol&amp;gt;</a>
<span class="sourceLineNo">128</span><a id="line.128">     *     &amp;lt;li&amp;gt;</a>
<span class="sourceLineNo">129</span><a id="line.129">     *     The class has a writable property with the same name as a column.</a>
<span class="sourceLineNo">130</span><a id="line.130">     *     The name comparison is case insensitive.</a>
<span class="sourceLineNo">131</span><a id="line.131">     *     &amp;lt;/li&amp;gt;</a>
<span class="sourceLineNo">132</span><a id="line.132">     *</a>
<span class="sourceLineNo">133</span><a id="line.133">     *     &amp;lt;li&amp;gt;</a>
<span class="sourceLineNo">134</span><a id="line.134">     *     The column type can be converted to the property's set method</a>
<span class="sourceLineNo">135</span><a id="line.135">     *     parameter type with a ResultSet.get* method.  If the conversion fails</a>
<span class="sourceLineNo">136</span><a id="line.136">     *     (ie. the property was an int and the column was a Timestamp) an</a>
<span class="sourceLineNo">137</span><a id="line.137">     *     SQLException is thrown.</a>
<span class="sourceLineNo">138</span><a id="line.138">     *     &amp;lt;/li&amp;gt;</a>
<span class="sourceLineNo">139</span><a id="line.139">     * &amp;lt;/ol&amp;gt;</a>
<span class="sourceLineNo">140</span><a id="line.140">     *</a>
<span class="sourceLineNo">141</span><a id="line.141">     * &amp;lt;p&amp;gt;</a>
<span class="sourceLineNo">142</span><a id="line.142">     * Primitive bean properties are set to their defaults when SQL NULL is</a>
<span class="sourceLineNo">143</span><a id="line.143">     * returned from the {@code ResultSet}.  Numeric fields are set to 0</a>
<span class="sourceLineNo">144</span><a id="line.144">     * and booleans are set to false.  Object bean properties are set to</a>
<span class="sourceLineNo">145</span><a id="line.145">     * {@code null} when SQL NULL is returned.  This is the same behavior</a>
<span class="sourceLineNo">146</span><a id="line.146">     * as the {@code ResultSet} get* methods.</a>
<span class="sourceLineNo">147</span><a id="line.147">     * &amp;lt;/p&amp;gt;</a>
<span class="sourceLineNo">148</span><a id="line.148">     * @param &lt;T&gt; The type of bean to create</a>
<span class="sourceLineNo">149</span><a id="line.149">     * @param rs ResultSet that supplies the bean data</a>
<span class="sourceLineNo">150</span><a id="line.150">     * @param type Class from which to create the bean instance</a>
<span class="sourceLineNo">151</span><a id="line.151">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">152</span><a id="line.152">     * @return the newly created bean</a>
<span class="sourceLineNo">153</span><a id="line.153">     */</a>
<span class="sourceLineNo">154</span><a id="line.154">    public &lt;T&gt; T toBean(final ResultSet rs, final Class&lt;? extends T&gt; type) throws SQLException {</a>
<span class="sourceLineNo">155</span><a id="line.155">        final T bean = this.newInstance(type);</a>
<span class="sourceLineNo">156</span><a id="line.156">        return this.populateBean(rs, bean);</a>
<span class="sourceLineNo">157</span><a id="line.157">    }</a>
<span class="sourceLineNo">158</span><a id="line.158"></a>
<span class="sourceLineNo">159</span><a id="line.159">    /**</a>
<span class="sourceLineNo">160</span><a id="line.160">     * Convert a {@code ResultSet} into a {@code List} of JavaBeans.</a>
<span class="sourceLineNo">161</span><a id="line.161">     * This implementation uses reflection and {@code BeanInfo} classes to</a>
<span class="sourceLineNo">162</span><a id="line.162">     * match column names to bean property names. Properties are matched to</a>
<span class="sourceLineNo">163</span><a id="line.163">     * columns based on several factors:</a>
<span class="sourceLineNo">164</span><a id="line.164">     * &amp;lt;br/&amp;gt;</a>
<span class="sourceLineNo">165</span><a id="line.165">     * &amp;lt;ol&amp;gt;</a>
<span class="sourceLineNo">166</span><a id="line.166">     *     &amp;lt;li&amp;gt;</a>
<span class="sourceLineNo">167</span><a id="line.167">     *     The class has a writable property with the same name as a column.</a>
<span class="sourceLineNo">168</span><a id="line.168">     *     The name comparison is case insensitive.</a>
<span class="sourceLineNo">169</span><a id="line.169">     *     &amp;lt;/li&amp;gt;</a>
<span class="sourceLineNo">170</span><a id="line.170">     *</a>
<span class="sourceLineNo">171</span><a id="line.171">     *     &amp;lt;li&amp;gt;</a>
<span class="sourceLineNo">172</span><a id="line.172">     *     The column type can be converted to the property's set method</a>
<span class="sourceLineNo">173</span><a id="line.173">     *     parameter type with a ResultSet.get* method.  If the conversion fails</a>
<span class="sourceLineNo">174</span><a id="line.174">     *     (ie. the property was an int and the column was a Timestamp) an</a>
<span class="sourceLineNo">175</span><a id="line.175">     *     SQLException is thrown.</a>
<span class="sourceLineNo">176</span><a id="line.176">     *     &amp;lt;/li&amp;gt;</a>
<span class="sourceLineNo">177</span><a id="line.177">     * &amp;lt;/ol&amp;gt;</a>
<span class="sourceLineNo">178</span><a id="line.178">     *</a>
<span class="sourceLineNo">179</span><a id="line.179">     * &lt;p&gt;</a>
<span class="sourceLineNo">180</span><a id="line.180">     * Primitive bean properties are set to their defaults when SQL NULL is</a>
<span class="sourceLineNo">181</span><a id="line.181">     * returned from the {@code ResultSet}.  Numeric fields are set to 0</a>
<span class="sourceLineNo">182</span><a id="line.182">     * and booleans are set to false.  Object bean properties are set to</a>
<span class="sourceLineNo">183</span><a id="line.183">     * {@code null} when SQL NULL is returned.  This is the same behavior</a>
<span class="sourceLineNo">184</span><a id="line.184">     * as the {@code ResultSet} get* methods.</a>
<span class="sourceLineNo">185</span><a id="line.185">     * &amp;lt;/p&amp;gt;</a>
<span class="sourceLineNo">186</span><a id="line.186">     * @param &lt;T&gt; The type of bean to create</a>
<span class="sourceLineNo">187</span><a id="line.187">     * @param rs ResultSet that supplies the bean data</a>
<span class="sourceLineNo">188</span><a id="line.188">     * @param type Class from which to create the bean instance</a>
<span class="sourceLineNo">189</span><a id="line.189">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">190</span><a id="line.190">     * @return the newly created List of beans</a>
<span class="sourceLineNo">191</span><a id="line.191">     */</a>
<span class="sourceLineNo">192</span><a id="line.192">    public &lt;T&gt; List&lt;T&gt; toBeanList(final ResultSet rs, final Class&lt;? extends T&gt; type) throws SQLException {</a>
<span class="sourceLineNo">193</span><a id="line.193">        final List&lt;T&gt; results = new ArrayList&lt;&gt;();</a>
<span class="sourceLineNo">194</span><a id="line.194"></a>
<span class="sourceLineNo">195</span><a id="line.195">        if (!rs.next()) {</a>
<span class="sourceLineNo">196</span><a id="line.196">            return results;</a>
<span class="sourceLineNo">197</span><a id="line.197">        }</a>
<span class="sourceLineNo">198</span><a id="line.198"></a>
<span class="sourceLineNo">199</span><a id="line.199">        final PropertyDescriptor[] props = this.propertyDescriptors(type);</a>
<span class="sourceLineNo">200</span><a id="line.200">        final ResultSetMetaData rsmd = rs.getMetaData();</a>
<span class="sourceLineNo">201</span><a id="line.201">        final int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);</a>
<span class="sourceLineNo">202</span><a id="line.202"></a>
<span class="sourceLineNo">203</span><a id="line.203">        do {</a>
<span class="sourceLineNo">204</span><a id="line.204">            results.add(this.createBean(rs, type, props, columnToProperty));</a>
<span class="sourceLineNo">205</span><a id="line.205">        } while (rs.next());</a>
<span class="sourceLineNo">206</span><a id="line.206"></a>
<span class="sourceLineNo">207</span><a id="line.207">        return results;</a>
<span class="sourceLineNo">208</span><a id="line.208">    }</a>
<span class="sourceLineNo">209</span><a id="line.209"></a>
<span class="sourceLineNo">210</span><a id="line.210">    /**</a>
<span class="sourceLineNo">211</span><a id="line.211">     * Creates a new object and initializes its fields from the ResultSet.</a>
<span class="sourceLineNo">212</span><a id="line.212">     * @param &lt;T&gt; The type of bean to create</a>
<span class="sourceLineNo">213</span><a id="line.213">     * @param rs The result set.</a>
<span class="sourceLineNo">214</span><a id="line.214">     * @param type The bean type (the return type of the object).</a>
<span class="sourceLineNo">215</span><a id="line.215">     * @param props The property descriptors.</a>
<span class="sourceLineNo">216</span><a id="line.216">     * @param columnToProperty The column indices in the result set.</a>
<span class="sourceLineNo">217</span><a id="line.217">     * @return An initialized object.</a>
<span class="sourceLineNo">218</span><a id="line.218">     * @throws SQLException if a database error occurs.</a>
<span class="sourceLineNo">219</span><a id="line.219">     */</a>
<span class="sourceLineNo">220</span><a id="line.220">    private &lt;T&gt; T createBean(final ResultSet rs, final Class&lt;T&gt; type,</a>
<span class="sourceLineNo">221</span><a id="line.221">                             final PropertyDescriptor[] props, final int[] columnToProperty)</a>
<span class="sourceLineNo">222</span><a id="line.222">    throws SQLException {</a>
<span class="sourceLineNo">223</span><a id="line.223"></a>
<span class="sourceLineNo">224</span><a id="line.224">        final T bean = this.newInstance(type);</a>
<span class="sourceLineNo">225</span><a id="line.225">        return populateBean(rs, bean, props, columnToProperty);</a>
<span class="sourceLineNo">226</span><a id="line.226">    }</a>
<span class="sourceLineNo">227</span><a id="line.227"></a>
<span class="sourceLineNo">228</span><a id="line.228">    /**</a>
<span class="sourceLineNo">229</span><a id="line.229">     * Initializes the fields of the provided bean from the ResultSet.</a>
<span class="sourceLineNo">230</span><a id="line.230">     * @param &lt;T&gt; The type of bean</a>
<span class="sourceLineNo">231</span><a id="line.231">     * @param rs The result set.</a>
<span class="sourceLineNo">232</span><a id="line.232">     * @param bean The bean to be populated.</a>
<span class="sourceLineNo">233</span><a id="line.233">     * @return An initialized object.</a>
<span class="sourceLineNo">234</span><a id="line.234">     * @throws SQLException if a database error occurs.</a>
<span class="sourceLineNo">235</span><a id="line.235">     */</a>
<span class="sourceLineNo">236</span><a id="line.236">    public &lt;T&gt; T populateBean(final ResultSet rs, final T bean) throws SQLException {</a>
<span class="sourceLineNo">237</span><a id="line.237">        final PropertyDescriptor[] props = this.propertyDescriptors(bean.getClass());</a>
<span class="sourceLineNo">238</span><a id="line.238">        final ResultSetMetaData rsmd = rs.getMetaData();</a>
<span class="sourceLineNo">239</span><a id="line.239">        final int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);</a>
<span class="sourceLineNo">240</span><a id="line.240"></a>
<span class="sourceLineNo">241</span><a id="line.241">        return populateBean(rs, bean, props, columnToProperty);</a>
<span class="sourceLineNo">242</span><a id="line.242">    }</a>
<span class="sourceLineNo">243</span><a id="line.243"></a>
<span class="sourceLineNo">244</span><a id="line.244">    /**</a>
<span class="sourceLineNo">245</span><a id="line.245">     * This method populates a bean from the ResultSet based upon the underlying meta-data.</a>
<span class="sourceLineNo">246</span><a id="line.246">     *</a>
<span class="sourceLineNo">247</span><a id="line.247">     * @param &lt;T&gt; The type of bean</a>
<span class="sourceLineNo">248</span><a id="line.248">     * @param rs The result set.</a>
<span class="sourceLineNo">249</span><a id="line.249">     * @param bean The bean to be populated.</a>
<span class="sourceLineNo">250</span><a id="line.250">     * @param props The property descriptors.</a>
<span class="sourceLineNo">251</span><a id="line.251">     * @param columnToProperty The column indices in the result set.</a>
<span class="sourceLineNo">252</span><a id="line.252">     * @return An initialized object.</a>
<span class="sourceLineNo">253</span><a id="line.253">     * @throws SQLException if a database error occurs.</a>
<span class="sourceLineNo">254</span><a id="line.254">     */</a>
<span class="sourceLineNo">255</span><a id="line.255">    private &lt;T&gt; T populateBean(final ResultSet rs, final T bean,</a>
<span class="sourceLineNo">256</span><a id="line.256">            final PropertyDescriptor[] props, final int[] columnToProperty)</a>
<span class="sourceLineNo">257</span><a id="line.257">            throws SQLException {</a>
<span class="sourceLineNo">258</span><a id="line.258"></a>
<span class="sourceLineNo">259</span><a id="line.259">        for (int i = 1; i &lt; columnToProperty.length; i++) {</a>
<span class="sourceLineNo">260</span><a id="line.260"></a>
<span class="sourceLineNo">261</span><a id="line.261">            if (columnToProperty[i] == PROPERTY_NOT_FOUND) {</a>
<span class="sourceLineNo">262</span><a id="line.262">                continue;</a>
<span class="sourceLineNo">263</span><a id="line.263">            }</a>
<span class="sourceLineNo">264</span><a id="line.264"></a>
<span class="sourceLineNo">265</span><a id="line.265">            final PropertyDescriptor prop = props[columnToProperty[i]];</a>
<span class="sourceLineNo">266</span><a id="line.266">            final Class&lt;?&gt; propType = prop.getPropertyType();</a>
<span class="sourceLineNo">267</span><a id="line.267"></a>
<span class="sourceLineNo">268</span><a id="line.268">            Object value = null;</a>
<span class="sourceLineNo">269</span><a id="line.269">            if (propType != null) {</a>
<span class="sourceLineNo">270</span><a id="line.270">                value = this.processColumn(rs, i, propType);</a>
<span class="sourceLineNo">271</span><a id="line.271"></a>
<span class="sourceLineNo">272</span><a id="line.272">                if (value == null &amp;&amp; propType.isPrimitive()) {</a>
<span class="sourceLineNo">273</span><a id="line.273">                    value = primitiveDefaults.get(propType);</a>
<span class="sourceLineNo">274</span><a id="line.274">                }</a>
<span class="sourceLineNo">275</span><a id="line.275">            }</a>
<span class="sourceLineNo">276</span><a id="line.276"></a>
<span class="sourceLineNo">277</span><a id="line.277">            this.callSetter(bean, prop, value);</a>
<span class="sourceLineNo">278</span><a id="line.278">        }</a>
<span class="sourceLineNo">279</span><a id="line.279"></a>
<span class="sourceLineNo">280</span><a id="line.280">        return bean;</a>
<span class="sourceLineNo">281</span><a id="line.281">    }</a>
<span class="sourceLineNo">282</span><a id="line.282"></a>
<span class="sourceLineNo">283</span><a id="line.283">    /**</a>
<span class="sourceLineNo">284</span><a id="line.284">     * Calls the setter method on the target object for the given property.</a>
<span class="sourceLineNo">285</span><a id="line.285">     * If no setter method exists for the property, this method does nothing.</a>
<span class="sourceLineNo">286</span><a id="line.286">     * @param target The object to set the property on.</a>
<span class="sourceLineNo">287</span><a id="line.287">     * @param prop The property to set.</a>
<span class="sourceLineNo">288</span><a id="line.288">     * @param value The value to pass into the setter.</a>
<span class="sourceLineNo">289</span><a id="line.289">     * @throws SQLException if an error occurs setting the property.</a>
<span class="sourceLineNo">290</span><a id="line.290">     */</a>
<span class="sourceLineNo">291</span><a id="line.291">    private void callSetter(final Object target, final PropertyDescriptor prop, Object value)</a>
<span class="sourceLineNo">292</span><a id="line.292">            throws SQLException {</a>
<span class="sourceLineNo">293</span><a id="line.293"></a>
<span class="sourceLineNo">294</span><a id="line.294">        final Method setter = getWriteMethod(target, prop, value);</a>
<span class="sourceLineNo">295</span><a id="line.295"></a>
<span class="sourceLineNo">296</span><a id="line.296">        if (setter == null || setter.getParameterTypes().length != 1) {</a>
<span class="sourceLineNo">297</span><a id="line.297">            return;</a>
<span class="sourceLineNo">298</span><a id="line.298">        }</a>
<span class="sourceLineNo">299</span><a id="line.299"></a>
<span class="sourceLineNo">300</span><a id="line.300">        try {</a>
<span class="sourceLineNo">301</span><a id="line.301">            final Class&lt;?&gt; firstParam = setter.getParameterTypes()[0];</a>
<span class="sourceLineNo">302</span><a id="line.302">            for (final PropertyHandler handler : propertyHandlers) {</a>
<span class="sourceLineNo">303</span><a id="line.303">                if (handler.match(firstParam, value)) {</a>
<span class="sourceLineNo">304</span><a id="line.304">                    value = handler.apply(firstParam, value);</a>
<span class="sourceLineNo">305</span><a id="line.305">                    break;</a>
<span class="sourceLineNo">306</span><a id="line.306">                }</a>
<span class="sourceLineNo">307</span><a id="line.307">            }</a>
<span class="sourceLineNo">308</span><a id="line.308"></a>
<span class="sourceLineNo">309</span><a id="line.309">            // Don't call setter if the value object isn't the right type</a>
<span class="sourceLineNo">310</span><a id="line.310">            if (this.isCompatibleType(value, firstParam)) {</a>
<span class="sourceLineNo">311</span><a id="line.311">                setter.invoke(target, value);</a>
<span class="sourceLineNo">312</span><a id="line.312">            } else {</a>
<span class="sourceLineNo">313</span><a id="line.313">              throw new SQLException(</a>
<span class="sourceLineNo">314</span><a id="line.314">                  "Cannot set " + prop.getName() + ": incompatible types, cannot convert "</a>
<span class="sourceLineNo">315</span><a id="line.315">                  + value.getClass().getName() + " to " + firstParam.getName());</a>
<span class="sourceLineNo">316</span><a id="line.316">                  // value cannot be null here because isCompatibleType allows null</a>
<span class="sourceLineNo">317</span><a id="line.317">            }</a>
<span class="sourceLineNo">318</span><a id="line.318"></a>
<span class="sourceLineNo">319</span><a id="line.319">        } catch (final IllegalArgumentException e) {</a>
<span class="sourceLineNo">320</span><a id="line.320">            throw new SQLException(</a>
<span class="sourceLineNo">321</span><a id="line.321">                "Cannot set " + prop.getName() + ": " + e.getMessage());</a>
<span class="sourceLineNo">322</span><a id="line.322"></a>
<span class="sourceLineNo">323</span><a id="line.323">        } catch (final IllegalAccessException e) {</a>
<span class="sourceLineNo">324</span><a id="line.324">            throw new SQLException(</a>
<span class="sourceLineNo">325</span><a id="line.325">                "Cannot set " + prop.getName() + ": " + e.getMessage());</a>
<span class="sourceLineNo">326</span><a id="line.326"></a>
<span class="sourceLineNo">327</span><a id="line.327">        } catch (final InvocationTargetException e) {</a>
<span class="sourceLineNo">328</span><a id="line.328">            throw new SQLException(</a>
<span class="sourceLineNo">329</span><a id="line.329">                "Cannot set " + prop.getName() + ": " + e.getMessage());</a>
<span class="sourceLineNo">330</span><a id="line.330">        }</a>
<span class="sourceLineNo">331</span><a id="line.331">    }</a>
<span class="sourceLineNo">332</span><a id="line.332"></a>
<span class="sourceLineNo">333</span><a id="line.333">    /**</a>
<span class="sourceLineNo">334</span><a id="line.334">     * ResultSet.getObject() returns an Integer object for an INT column.  The</a>
<span class="sourceLineNo">335</span><a id="line.335">     * setter method for the property might take an Integer or a primitive int.</a>
<span class="sourceLineNo">336</span><a id="line.336">     * This method returns true if the value can be successfully passed into</a>
<span class="sourceLineNo">337</span><a id="line.337">     * the setter method.  Remember, Method.invoke() handles the unwrapping</a>
<span class="sourceLineNo">338</span><a id="line.338">     * of Integer into an int.</a>
<span class="sourceLineNo">339</span><a id="line.339">     *</a>
<span class="sourceLineNo">340</span><a id="line.340">     * @param value The value to be passed into the setter method.</a>
<span class="sourceLineNo">341</span><a id="line.341">     * @param type The setter's parameter type (non-null)</a>
<span class="sourceLineNo">342</span><a id="line.342">     * @return boolean True if the value is compatible (null =&gt; true)</a>
<span class="sourceLineNo">343</span><a id="line.343">     */</a>
<span class="sourceLineNo">344</span><a id="line.344">    private boolean isCompatibleType(final Object value, final Class&lt;?&gt; type) {</a>
<span class="sourceLineNo">345</span><a id="line.345">        // Do object check first, then primitives</a>
<span class="sourceLineNo">346</span><a id="line.346">        if (value == null || type.isInstance(value) || matchesPrimitive(type, value.getClass())) {</a>
<span class="sourceLineNo">347</span><a id="line.347">            return true;</a>
<span class="sourceLineNo">348</span><a id="line.348"></a>
<span class="sourceLineNo">349</span><a id="line.349">        }</a>
<span class="sourceLineNo">350</span><a id="line.350">        return false;</a>
<span class="sourceLineNo">351</span><a id="line.351"></a>
<span class="sourceLineNo">352</span><a id="line.352">    }</a>
<span class="sourceLineNo">353</span><a id="line.353"></a>
<span class="sourceLineNo">354</span><a id="line.354">    /**</a>
<span class="sourceLineNo">355</span><a id="line.355">     * Check whether a value is of the same primitive type as {@code targetType}.</a>
<span class="sourceLineNo">356</span><a id="line.356">     *</a>
<span class="sourceLineNo">357</span><a id="line.357">     * @param targetType The primitive type to target.</a>
<span class="sourceLineNo">358</span><a id="line.358">     * @param valueType The value to match to the primitive type.</a>
<span class="sourceLineNo">359</span><a id="line.359">     * @return Whether {@code valueType} can be coerced (e.g. autoboxed) into {@code targetType}.</a>
<span class="sourceLineNo">360</span><a id="line.360">     */</a>
<span class="sourceLineNo">361</span><a id="line.361">    private boolean matchesPrimitive(final Class&lt;?&gt; targetType, final Class&lt;?&gt; valueType) {</a>
<span class="sourceLineNo">362</span><a id="line.362">        if (!targetType.isPrimitive()) {</a>
<span class="sourceLineNo">363</span><a id="line.363">            return false;</a>
<span class="sourceLineNo">364</span><a id="line.364">        }</a>
<span class="sourceLineNo">365</span><a id="line.365"></a>
<span class="sourceLineNo">366</span><a id="line.366">        try {</a>
<span class="sourceLineNo">367</span><a id="line.367">            // see if there is a "TYPE" field.  This is present for primitive wrappers.</a>
<span class="sourceLineNo">368</span><a id="line.368">            final Field typeField = valueType.getField("TYPE");</a>
<span class="sourceLineNo">369</span><a id="line.369">            final Object primitiveValueType = typeField.get(valueType);</a>
<span class="sourceLineNo">370</span><a id="line.370"></a>
<span class="sourceLineNo">371</span><a id="line.371">            if (targetType == primitiveValueType) {</a>
<span class="sourceLineNo">372</span><a id="line.372">                return true;</a>
<span class="sourceLineNo">373</span><a id="line.373">            }</a>
<span class="sourceLineNo">374</span><a id="line.374">        } catch (final NoSuchFieldException e) {</a>
<span class="sourceLineNo">375</span><a id="line.375">            // lacking the TYPE field is a good sign that we're not working with a primitive wrapper.</a>
<span class="sourceLineNo">376</span><a id="line.376">            // we can't match for compatibility</a>
<span class="sourceLineNo">377</span><a id="line.377">        } catch (final IllegalAccessException e) {</a>
<span class="sourceLineNo">378</span><a id="line.378">            // an inaccessible TYPE field is a good sign that we're not working with a primitive wrapper.</a>
<span class="sourceLineNo">379</span><a id="line.379">            // nothing to do.  we can't match for compatibility</a>
<span class="sourceLineNo">380</span><a id="line.380">        }</a>
<span class="sourceLineNo">381</span><a id="line.381">        return false;</a>
<span class="sourceLineNo">382</span><a id="line.382">    }</a>
<span class="sourceLineNo">383</span><a id="line.383"></a>
<span class="sourceLineNo">384</span><a id="line.384">    /**</a>
<span class="sourceLineNo">385</span><a id="line.385">     * Get the write method to use when setting {@code value} to the {@code target}.</a>
<span class="sourceLineNo">386</span><a id="line.386">     *</a>
<span class="sourceLineNo">387</span><a id="line.387">     * @param target Object where the write method will be called.</a>
<span class="sourceLineNo">388</span><a id="line.388">     * @param prop   BeanUtils information.</a>
<span class="sourceLineNo">389</span><a id="line.389">     * @param value  The value that will be passed to the write method.</a>
<span class="sourceLineNo">390</span><a id="line.390">     * @return The {@link java.lang.reflect.Method} to call on {@code target} to write {@code value} or {@code null} if</a>
<span class="sourceLineNo">391</span><a id="line.391">     *         there is no suitable write method.</a>
<span class="sourceLineNo">392</span><a id="line.392">     */</a>
<span class="sourceLineNo">393</span><a id="line.393">    protected Method getWriteMethod(final Object target, final PropertyDescriptor prop, final Object value) {</a>
<span class="sourceLineNo">394</span><a id="line.394">        final Method method = prop.getWriteMethod();</a>
<span class="sourceLineNo">395</span><a id="line.395">        return method;</a>
<span class="sourceLineNo">396</span><a id="line.396">    }</a>
<span class="sourceLineNo">397</span><a id="line.397"></a>
<span class="sourceLineNo">398</span><a id="line.398">    /**</a>
<span class="sourceLineNo">399</span><a id="line.399">     * Factory method that returns a new instance of the given Class.  This</a>
<span class="sourceLineNo">400</span><a id="line.400">     * is called at the start of the bean creation process and may be</a>
<span class="sourceLineNo">401</span><a id="line.401">     * overridden to provide custom behavior like returning a cached bean</a>
<span class="sourceLineNo">402</span><a id="line.402">     * instance.</a>
<span class="sourceLineNo">403</span><a id="line.403">     * @param &lt;T&gt; The type of object to create</a>
<span class="sourceLineNo">404</span><a id="line.404">     * @param c The Class to create an object from.</a>
<span class="sourceLineNo">405</span><a id="line.405">     * @return A newly created object of the Class.</a>
<span class="sourceLineNo">406</span><a id="line.406">     * @throws SQLException if creation failed.</a>
<span class="sourceLineNo">407</span><a id="line.407">     */</a>
<span class="sourceLineNo">408</span><a id="line.408">    protected &lt;T&gt; T newInstance(final Class&lt;T&gt; c) throws SQLException {</a>
<span class="sourceLineNo">409</span><a id="line.409">        try {</a>
<span class="sourceLineNo">410</span><a id="line.410">            return c.getDeclaredConstructor().newInstance();</a>
<span class="sourceLineNo">411</span><a id="line.411"></a>
<span class="sourceLineNo">412</span><a id="line.412">        } catch (final IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException e) {</a>
<span class="sourceLineNo">413</span><a id="line.413">            throw new SQLException(</a>
<span class="sourceLineNo">414</span><a id="line.414">                "Cannot create " + c.getName() + ": " + e.getMessage());</a>
<span class="sourceLineNo">415</span><a id="line.415">        }</a>
<span class="sourceLineNo">416</span><a id="line.416">    }</a>
<span class="sourceLineNo">417</span><a id="line.417"></a>
<span class="sourceLineNo">418</span><a id="line.418">    /**</a>
<span class="sourceLineNo">419</span><a id="line.419">     * Returns a PropertyDescriptor[] for the given Class.</a>
<span class="sourceLineNo">420</span><a id="line.420">     *</a>
<span class="sourceLineNo">421</span><a id="line.421">     * @param c The Class to retrieve PropertyDescriptors for.</a>
<span class="sourceLineNo">422</span><a id="line.422">     * @return A PropertyDescriptor[] describing the Class.</a>
<span class="sourceLineNo">423</span><a id="line.423">     * @throws SQLException if introspection failed.</a>
<span class="sourceLineNo">424</span><a id="line.424">     */</a>
<span class="sourceLineNo">425</span><a id="line.425">    private PropertyDescriptor[] propertyDescriptors(final Class&lt;?&gt; c)</a>
<span class="sourceLineNo">426</span><a id="line.426">        throws SQLException {</a>
<span class="sourceLineNo">427</span><a id="line.427">        // Introspector caches BeanInfo classes for better performance</a>
<span class="sourceLineNo">428</span><a id="line.428">        BeanInfo beanInfo = null;</a>
<span class="sourceLineNo">429</span><a id="line.429">        try {</a>
<span class="sourceLineNo">430</span><a id="line.430">            beanInfo = Introspector.getBeanInfo(c);</a>
<span class="sourceLineNo">431</span><a id="line.431"></a>
<span class="sourceLineNo">432</span><a id="line.432">        } catch (final IntrospectionException e) {</a>
<span class="sourceLineNo">433</span><a id="line.433">            throw new SQLException(</a>
<span class="sourceLineNo">434</span><a id="line.434">                "Bean introspection failed: " + e.getMessage());</a>
<span class="sourceLineNo">435</span><a id="line.435">        }</a>
<span class="sourceLineNo">436</span><a id="line.436"></a>
<span class="sourceLineNo">437</span><a id="line.437">        return beanInfo.getPropertyDescriptors();</a>
<span class="sourceLineNo">438</span><a id="line.438">    }</a>
<span class="sourceLineNo">439</span><a id="line.439"></a>
<span class="sourceLineNo">440</span><a id="line.440">    /**</a>
<span class="sourceLineNo">441</span><a id="line.441">     * The positions in the returned array represent column numbers.  The</a>
<span class="sourceLineNo">442</span><a id="line.442">     * values stored at each position represent the index in the</a>
<span class="sourceLineNo">443</span><a id="line.443">     * {@code PropertyDescriptor[]} for the bean property that matches</a>
<span class="sourceLineNo">444</span><a id="line.444">     * the column name.  If no bean property was found for a column, the</a>
<span class="sourceLineNo">445</span><a id="line.445">     * position is set to {@code PROPERTY_NOT_FOUND}.</a>
<span class="sourceLineNo">446</span><a id="line.446">     *</a>
<span class="sourceLineNo">447</span><a id="line.447">     * @param rsmd The {@code ResultSetMetaData} containing column</a>
<span class="sourceLineNo">448</span><a id="line.448">     * information.</a>
<span class="sourceLineNo">449</span><a id="line.449">     *</a>
<span class="sourceLineNo">450</span><a id="line.450">     * @param props The bean property descriptors.</a>
<span class="sourceLineNo">451</span><a id="line.451">     *</a>
<span class="sourceLineNo">452</span><a id="line.452">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">453</span><a id="line.453">     *</a>
<span class="sourceLineNo">454</span><a id="line.454">     * @return An int[] with column index to property index mappings.  The 0th</a>
<span class="sourceLineNo">455</span><a id="line.455">     * element is meaningless because JDBC column indexing starts at 1.</a>
<span class="sourceLineNo">456</span><a id="line.456">     */</a>
<span class="sourceLineNo">457</span><a id="line.457">    protected int[] mapColumnsToProperties(final ResultSetMetaData rsmd,</a>
<span class="sourceLineNo">458</span><a id="line.458">            final PropertyDescriptor[] props) throws SQLException {</a>
<span class="sourceLineNo">459</span><a id="line.459"></a>
<span class="sourceLineNo">460</span><a id="line.460">        final int cols = rsmd.getColumnCount();</a>
<span class="sourceLineNo">461</span><a id="line.461">        final int[] columnToProperty = new int[cols + 1];</a>
<span class="sourceLineNo">462</span><a id="line.462">        Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND);</a>
<span class="sourceLineNo">463</span><a id="line.463"></a>
<span class="sourceLineNo">464</span><a id="line.464">        for (int col = 1; col &lt;= cols; col++) {</a>
<span class="sourceLineNo">465</span><a id="line.465">            String columnName = rsmd.getColumnLabel(col);</a>
<span class="sourceLineNo">466</span><a id="line.466">            if (null == columnName || 0 == columnName.length()) {</a>
<span class="sourceLineNo">467</span><a id="line.467">              columnName = rsmd.getColumnName(col);</a>
<span class="sourceLineNo">468</span><a id="line.468">            }</a>
<span class="sourceLineNo">469</span><a id="line.469">            String propertyName = columnToPropertyOverrides.get(columnName);</a>
<span class="sourceLineNo">470</span><a id="line.470">            if (propertyName == null) {</a>
<span class="sourceLineNo">471</span><a id="line.471">                propertyName = columnName;</a>
<span class="sourceLineNo">472</span><a id="line.472">            }</a>
<span class="sourceLineNo">473</span><a id="line.473">            for (int i = 0; i &lt; props.length; i++) {</a>
<span class="sourceLineNo">474</span><a id="line.474"></a>
<span class="sourceLineNo">475</span><a id="line.475">                PropertyDescriptor prop = props[i];</a>
<span class="sourceLineNo">476</span><a id="line.476">                Column column = prop.getReadMethod().getAnnotation(Column.class);</a>
<span class="sourceLineNo">477</span><a id="line.477">                String propertyColumnName = null;</a>
<span class="sourceLineNo">478</span><a id="line.478">                if (column != null) {</a>
<span class="sourceLineNo">479</span><a id="line.479">                    propertyColumnName = column.name();</a>
<span class="sourceLineNo">480</span><a id="line.480">                } else {</a>
<span class="sourceLineNo">481</span><a id="line.481">                    propertyColumnName = prop.getName();</a>
<span class="sourceLineNo">482</span><a id="line.482">                }</a>
<span class="sourceLineNo">483</span><a id="line.483">                if (propertyName.equalsIgnoreCase(propertyColumnName)) {</a>
<span class="sourceLineNo">484</span><a id="line.484">                    columnToProperty[col] = i;</a>
<span class="sourceLineNo">485</span><a id="line.485">                    break;</a>
<span class="sourceLineNo">486</span><a id="line.486">                }</a>
<span class="sourceLineNo">487</span><a id="line.487">            }</a>
<span class="sourceLineNo">488</span><a id="line.488">        }</a>
<span class="sourceLineNo">489</span><a id="line.489"></a>
<span class="sourceLineNo">490</span><a id="line.490">        return columnToProperty;</a>
<span class="sourceLineNo">491</span><a id="line.491">    }</a>
<span class="sourceLineNo">492</span><a id="line.492"></a>
<span class="sourceLineNo">493</span><a id="line.493">    /**</a>
<span class="sourceLineNo">494</span><a id="line.494">     * Convert a {@code ResultSet} column into an object.  Simple</a>
<span class="sourceLineNo">495</span><a id="line.495">     * implementations could just call {@code rs.getObject(index)} while</a>
<span class="sourceLineNo">496</span><a id="line.496">     * more complex implementations could perform type manipulation to match</a>
<span class="sourceLineNo">497</span><a id="line.497">     * the column's type to the bean property type.</a>
<span class="sourceLineNo">498</span><a id="line.498">     *</a>
<span class="sourceLineNo">499</span><a id="line.499">     * &lt;p&gt;</a>
<span class="sourceLineNo">500</span><a id="line.500">     * This implementation calls the appropriate {@code ResultSet} getter</a>
<span class="sourceLineNo">501</span><a id="line.501">     * method for the given property type to perform the type conversion.  If</a>
<span class="sourceLineNo">502</span><a id="line.502">     * the property type doesn't match one of the supported</a>
<span class="sourceLineNo">503</span><a id="line.503">     * {@code ResultSet} types, {@code getObject} is called.</a>
<span class="sourceLineNo">504</span><a id="line.504">     * &lt;/p&gt;</a>
<span class="sourceLineNo">505</span><a id="line.505">     *</a>
<span class="sourceLineNo">506</span><a id="line.506">     * @param rs The {@code ResultSet} currently being processed.  It is</a>
<span class="sourceLineNo">507</span><a id="line.507">     * positioned on a valid row before being passed into this method.</a>
<span class="sourceLineNo">508</span><a id="line.508">     *</a>
<span class="sourceLineNo">509</span><a id="line.509">     * @param index The current column index being processed.</a>
<span class="sourceLineNo">510</span><a id="line.510">     *</a>
<span class="sourceLineNo">511</span><a id="line.511">     * @param propType The bean property type that this column needs to be</a>
<span class="sourceLineNo">512</span><a id="line.512">     * converted into.</a>
<span class="sourceLineNo">513</span><a id="line.513">     *</a>
<span class="sourceLineNo">514</span><a id="line.514">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">515</span><a id="line.515">     *</a>
<span class="sourceLineNo">516</span><a id="line.516">     * @return The object from the {@code ResultSet} at the given column</a>
<span class="sourceLineNo">517</span><a id="line.517">     * index after optional type processing or {@code null} if the column</a>
<span class="sourceLineNo">518</span><a id="line.518">     * value was SQL NULL.</a>
<span class="sourceLineNo">519</span><a id="line.519">     */</a>
<span class="sourceLineNo">520</span><a id="line.520">    protected Object processColumn(final ResultSet rs, final int index, final Class&lt;?&gt; propType)</a>
<span class="sourceLineNo">521</span><a id="line.521">        throws SQLException {</a>
<span class="sourceLineNo">522</span><a id="line.522"></a>
<span class="sourceLineNo">523</span><a id="line.523">        Object retval = rs.getObject(index);</a>
<span class="sourceLineNo">524</span><a id="line.524"></a>
<span class="sourceLineNo">525</span><a id="line.525">        if ( !propType.isPrimitive() &amp;&amp; retval == null ) {</a>
<span class="sourceLineNo">526</span><a id="line.526">            return null;</a>
<span class="sourceLineNo">527</span><a id="line.527">        }</a>
<span class="sourceLineNo">528</span><a id="line.528"></a>
<span class="sourceLineNo">529</span><a id="line.529">        for (final ColumnHandler handler : columnHandlers) {</a>
<span class="sourceLineNo">530</span><a id="line.530">            if (handler.match(propType)) {</a>
<span class="sourceLineNo">531</span><a id="line.531">                retval = handler.apply(rs, index);</a>
<span class="sourceLineNo">532</span><a id="line.532">                break;</a>
<span class="sourceLineNo">533</span><a id="line.533">            }</a>
<span class="sourceLineNo">534</span><a id="line.534">        }</a>
<span class="sourceLineNo">535</span><a id="line.535"></a>
<span class="sourceLineNo">536</span><a id="line.536">        return retval;</a>
<span class="sourceLineNo">537</span><a id="line.537"></a>
<span class="sourceLineNo">538</span><a id="line.538">    }</a>
<span class="sourceLineNo">539</span><a id="line.539"></a>
<span class="sourceLineNo">540</span><a id="line.540">}</a>




























































</pre>
</div>
</main>
</body>
</html>
